﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Resco.Controls.DetailView;

namespace AsTekMobile
{
    public partial class frmMontaj : Form
    {
        public Montaj SecilenMontaj { get; set; }

        public frmMontaj()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (detailView1.CurrentPageIndex < detailView1.Pages.Count - 1)
            {
                detailView1.Pages[detailView1.CurrentPageIndex + 1].Show();
            }
            else
                detailView1.Pages[0].Show();
        }

        private void frmMontaj_Load(object sender, EventArgs e)
        {
            try
            {
                EvrakAc();
            }
            catch (Exception ex)
            {
                ErrorMgr.Opps(ex);
            }
        }

        private void EvrakAc()
        {
            var firm = Database.ctor.FirmwareTanimlari;
            var mrk = Database.ctor.Markalar;
            var sis = Database.ctor.SistemTanimlari;
            var kpm = Database.ctor.KapamaSebepleri;

            cmbFirmware.DataSource = firm;
            cmbFirmware.DisplayMember = "Adi";

            cmbMarka.DataSource = mrk;
            cmbMarka.DisplayMember = "MarkaAdi";

            cmbSistem.DataSource = sis;
            cmbSistem.DisplayMember = "Adi";

            cmbKapanisNedeni.DataSource = kpm;
            cmbKapanisNedeni.DisplayMember = "Adi";

            var ozellik = Database.ctor.OzellikTanimlari;
            var aracoz = Database.ctor.AracOzellikleri(SecilenMontaj.AracID);
            int idx = 1;
            foreach (var item in ozellik)
            {
                for (int i = 0; i < detailView1.Items.Count; i++)
                {
                    if (detailView1.Items[i].Name == "txtEkOzellik" + idx)
                    {
                        ItemComboBox txt = (ItemComboBox)detailView1.Items[i];
                        idx++;
                        AracOzellik oz = aracoz.Where(x => x.OzellikID == item.ID).FirstOrDefault();
                        if (oz != null)
                        {
                            if (item.Adi.IndexOf(':') > -1)
                            {
                                txt.Label = item.Adi.Split(':')[0].Trim();
                                string[] dizi = item.Adi.Split(':')[1].Trim().Split(',');
                                for (int x = 0; x < dizi.Length; x++)
                                {
                                    txt.Add(dizi[x]);
                                }
                            }
                            else
                            {
                                txt.Label = item.Adi;
                                txt.Add(oz.Deger);
                            }
                            txt.Text = oz.Deger;
                            txt.Tag = oz;
                        }
                        else
                        {
                            if (item.Adi.IndexOf(':') > -1)
                            {
                                txt.Label = item.Adi.Split(':')[0].Trim();
                                string[] dizi = item.Adi.Split(':')[1].Trim().Split(',');
                                for (int x = 0; x < dizi.Length; x++)
                                {
                                    txt.Add(dizi[x]);
                                }
                            }
                            else
                            {
                                txt.Label = item.Adi;
                                txt.Add("");
                            }
                            txt.Text = "";
                            txt.Tag = new AracOzellik() { Deger = "", ID = -1, OzellikID = item.ID };
                        }
                        break;
                    }
                }
            }
            txtGrup.Text = SecilenMontaj.Grup;
            txtFirma.Text = SecilenMontaj.Firma;
            txtAdres.Text = SecilenMontaj.Adres;
            txtMusteri.Text = SecilenMontaj.Musteri;
            txtTelefon.Text = SecilenMontaj.Telefon;

            for (int i = 0; i < mrk.Count; i++)
            {
                if (mrk[i].ID == SecilenMontaj.MarkaID)
                {
                    cmbMarka.SelectedIndex = i;
                    break;
                }
            }
            for (int i = 0; i < firm.Count; i++)
            {
                if (firm[i].ID == SecilenMontaj.FirmwareID)
                {
                    cmbFirmware.SelectedIndex = i;
                    break;
                }
            }

            for (int i = 0; i < sis.Count; i++)
            {
                if (sis[i].ID == SecilenMontaj.SistemID)
                {
                    cmbSistem.SelectedIndex = i;
                    break;
                }
            }
            dteMontajTarihi.DateTime = SecilenMontaj.MontajTarihi;
            txtPlaka.Text = SecilenMontaj.Plaka;
            txtSasi.Text = SecilenMontaj.SasiNo;
            txtKoltuk.NumericValue = SecilenMontaj.KoltukAdet;
            txtMontajYeri.Text = SecilenMontaj.MontajYeri;
            txtYil.NumericValue = SecilenMontaj.UretimYili;
            txtServis.Text = SecilenMontaj.Servis;
            txtNot.Text = SecilenMontaj.Not;
            txtTeslimat.Text = SecilenMontaj.TeslimAlan;

            var hubs = Database.ctor.AracHubYerleri(SecilenMontaj.AracID);
            foreach (var item in hubs)
            {
                foreach (Item c in detailView1.Items)
                {
                    if (c.Name == "txtHub" + item.HubKoltukNo)
                    {
                        ItemMaskedTextBox txt = (ItemMaskedTextBox)c;
                        txt.Label = item.HubKoltukNo.ToString();
                        txt.Text = item.MonitorKoltukNo1.ToString("00") + "-" + item.MonitorKoltukNo2.ToString("00") + "-" + item.MonitorKoltukNo3.ToString("00") + "-" + item.MonitorKoltukNo4.ToString("00") + "-" + item.MonitorKoltukNo5.ToString("00") + "-" + item.MonitorKoltukNo6.ToString("00") + "-" + item.MonitorKoltukNo7.ToString("00");
                        txt.Tag = item.ID;
                    }
                }
            }
        }

        private void btnKapat_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult rs = MessageBox.Show("Montajı kaydetmek istermisiniz !", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (rs == DialogResult.Yes)
                {
                    Save();
                }
                this.Close();
            }
            catch (Exception ex)
            {
                ErrorMgr.Opps(ex);
            }
        }

        private void Save()
        {
            Montaj mnt = new Montaj();
            mnt.ID = SecilenMontaj.ID;
            mnt.AracID = SecilenMontaj.AracID;
            mnt.Tarih = SecilenMontaj.Tarih;
            int idx = 1;
            for (int i = 0; i < detailView1.Items.Count; i++)
            {
                if (detailView1.Items[i].Name == "txtEkOzellik" + idx)
                {
                    ItemComboBox txt = (ItemComboBox)detailView1.Items[i];
                    if (txt.Tag != null)
                    {
                        AracOzellik oz = (AracOzellik)txt.Tag;
                        mnt.EkOzellikler.Add(new AracOzellik()
                        {
                            Deger = txt.Text,
                            ID = oz.ID,
                            OzellikID = oz.OzellikID
                        });
                    }
                    idx++;
                }
            }

            mnt.Grup = txtGrup.Text;
            mnt.Firma = txtFirma.Text;
            mnt.Adres = txtAdres.Text;
            mnt.Musteri = txtMusteri.Text;
            mnt.Telefon = txtTelefon.Text;

            if (cmbMarka.SelectedIndex > -1)
                mnt.MarkaID = ((Marka)cmbMarka.SelectedItem).ID;
            else
                mnt.MarkaID = -1;

            if (cmbFirmware.SelectedIndex > -1)
                mnt.FirmwareID = ((Firmware)cmbFirmware.SelectedItem).ID;
            else
                mnt.FirmwareID = -1;

            if (cmbSistem.SelectedIndex > -1)
                mnt.SistemID = ((Sistem)cmbSistem.SelectedItem).ID;
            else
                mnt.SistemID = -1;

            mnt.MontajTarihi = dteMontajTarihi.DateTime;
            mnt.Plaka = txtPlaka.Text;
            mnt.SasiNo = txtSasi.Text;
            mnt.KoltukAdet = Cevir.Parse<int>(txtKoltuk.NumericValue);
            mnt.MontajYeri = txtMontajYeri.Text;
            mnt.UretimYili = Cevir.Parse<int>(txtYil.NumericValue);
            mnt.Servis = txtServis.Text;
            mnt.Not = txtNot.Text;
            mnt.TeslimAlan = txtTeslimat.Text;

            if (cmbKapanisNedeni.SelectedIndex > -1)
                mnt.KapamaNedenID = ((KapamaSebep)cmbKapanisNedeni.SelectedItem).ID;
            else
                mnt.KapamaNedenID = -1;


            idx = 1;
            foreach (Item c in detailView1.Items)
            {
                if (c.Name == "txtHub" + idx)
                {
                    ItemMaskedTextBox txt = (ItemMaskedTextBox)c;
                    string[] dizi = txt.Text.Split('-');
                    mnt.HubYerleri.Add(new HubYeri()
                    {
                        ID = Cevir.Parse<int>(txt.Tag),
                        HubKoltukNo = idx,
                        MonitorKoltukNo1 = Cevir.Parse<int>(dizi[0]),
                        MonitorKoltukNo2 = Cevir.Parse<int>(dizi[1]),
                        MonitorKoltukNo3 = Cevir.Parse<int>(dizi[2]),
                        MonitorKoltukNo4 = Cevir.Parse<int>(dizi[3]),
                        MonitorKoltukNo5 = Cevir.Parse<int>(dizi[4]),
                        MonitorKoltukNo6 = Cevir.Parse<int>(dizi[5]),
                        MonitorKoltukNo7 = Cevir.Parse<int>(dizi[6]),
                    });
                    idx++;
                }
            }
            Database.ctor.SaveMontaj(mnt);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (detailView1.CurrentPageIndex == 0)
            {
                detailView1.Pages[detailView1.Pages.Count - 1].Show();
            }
            else
                detailView1.Pages[detailView1.CurrentPageIndex - 1].Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Sip.Visible)
            {
                Sip.Visible = false;
            }
            else
            {
                Sip.Visible = true;
            }
        }
    }
}